package com.photo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photo.auto.JSONAuthentication;
import com.photo.entity.PhComment;
import com.photo.entity.PhPhoto;
import com.photo.mapper.PhCommentMapper;
import com.photo.mapper.PhPhotoMapper;
import com.photo.mapper.PhUserMapper;
import com.photo.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author photo
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/ph-comment")
public class PhCommentController extends JSONAuthentication {
    @Autowired
    PhCommentMapper phCommentMapper;
    @Autowired
    PhUserMapper phUserMapper;
    @Autowired
    PhPhotoMapper phPhotoMapper;
    @ApiOperation(value = "添加评论")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("content") String content,@RequestParam("phId") int phId) throws Exception {
        PhComment phComment=new PhComment();
        phComment.setUserId(userId);
        phComment.setContent(content);
        phComment.setPhId(phId);
        if (phCommentMapper.insert(phComment) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除评论")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = phCommentMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改评论状态")
    @PostMapping("/updStatus")
    public void updStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status") String status) throws Exception {
        PhComment phComment = phCommentMapper.selectById(id);
        phComment.setStatus(status);
        if (phCommentMapper.updateById(phComment) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找相册的评论")
    @PostMapping("/findAllPhoto")
    public void findAllPhoto(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("phId") int phId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("ph_id",phId);
        Page<PhComment> page = new Page<>(current, size);
        Page<PhComment> pages = phCommentMapper.selectPage(page, wrapper);
        List<PhComment> list = pages.getRecords();
        int total = phCommentMapper.selectCount(wrapper);
        for(PhComment a:list){
            if(a.getPhId()!=null){
                a.setPhoto(phPhotoMapper.selectById(a.getPhId()));
            }
         if(a.getUserId()!=null){
             a.setPhUser(phUserMapper.selectById(a.getUserId()));
         }
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有用户");
        this.WriteJSON(request, response, error);
    }
}

